package cn.lyjuan.archetypes.bms.web.controller;


import cn.lyjuan.archetypes.bms.web.bean.vo.BaseResp;
import cn.lyjuan.archetypes.bms.web.bean.vo.PagerResp;
import cn.lyjuan.archetypes.bms.web.controller.base.SecurityController;
import cn.lyjuan.archetypes.bms.common.entity.MgActionGroup;
import cn.lyjuan.archetypes.bms.web.service.ActionGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by ly on 2015/3/3.
 */
@Controller
@RequestMapping("/group")
public class ActionGroupController extends SecurityController
{

    @Autowired
    @Qualifier("mgActionGroupService")
    private ActionGroupService actionGroupSer;

    /**
     * 选择组
     * @return
     */
    @RequestMapping("selectGroup")
    public void selectGroup(Model model)
    {
        Map<String, String> param = this.paramsToMap();
        // 搜索组
        List<MgActionGroup> list = actionGroupSer.queryGroups(param, this.getPn(), this.getPs());

        this.pkgPage(list);
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView toList()
    {
        return mv("/group/group_list");
    }

    /**
     * 查询组
     * @return
     */
    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ResponseBody
    public PagerResp<MgActionGroup> list(Model model)
//    public String groupList(Model model)
    {
        Map<String, String> param = this.paramsToMap();
        // 搜索组
        List<MgActionGroup> list = actionGroupSer.queryGroups(param, this.getPn(), this.getPs());

        PagerResp<MgActionGroup> data = BaseResp.resp(list);
//        model.addAttribute("groups", data);
//        return "/group/group_list";
        return data;


    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public ModelAndView toAdd()
    {
        return mv("/group/group_edit");
    }

    /**
     * 增加组信息
     * @return
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp add(MgActionGroup group, HttpServletRequest req, HttpServletResponse resp)
    {
        actionGroupSer.addGroup(group);

        return BaseResp.resp();
    }

    /**
     * 请求修改组页面
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public ModelAndView toEdit(Integer id)
    {
        MgActionGroup group = actionGroupSer.queryGroupById(id);

        BaseResp<MgActionGroup> base = BaseResp.resp(group);

        return mv("/group/group_edit", base);
    }

    /**
     * 修改组信息
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp edit(MgActionGroup group)
    {
        actionGroupSer.updateActionGroup(group);

        return BaseResp.resp();
    }

    /**
     * 锁定组
     * @return
     */
    @RequestMapping("status")
    @ResponseBody
    public BaseResp status(Integer id, Boolean status)
    {
        actionGroupSer.updateStatus(id, status);

        return BaseResp.resp();
    }
}
